package eft.android.view;

import java.util.ArrayList;
import java.util.Calendar;

import me.croute.calendarexample.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;
import eft.android.controller.Controller;

/*
 */
public class CalendarViewActivity extends Activity implements OnItemClickListener, OnClickListener
{
	public static int SUNDAY 		= 1;
	public static int MONDAY 		= 2;
	public static int TUESDAY 		= 3;
	public static int WEDNSESDAY 	= 4;
	public static int THURSDAY 		= 5;
	public static int FRIDAY 		= 6;
	public static int SATURDAY 		= 7;
	public static int dayOfToday = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
	public static int monthOfToday = Calendar.getInstance().get(Calendar.MONTH);
	public static int yearOfToday = Calendar.getInstance().get(Calendar.YEAR);


	private TextView mTvCalendarTitle;
	private GridView mGvCalendar;


	// must set controller
	private Controller controller;

	private ArrayList<DayInfo> mDayList;
	private CalendarAdapter mCalendarAdapter;

	Calendar mLastMonthCalendar;
	Calendar mThisMonthCalendar;
	Calendar mNextMonthCalendar;
	String [] dayText = {"SUN","MON","TUE","WED","THU","FRI","SAT"};

	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.calendarviewactivity);

		controller = Controller.getInstance();

		Button bLastMonth = (Button)findViewById(R.id.gv_calendar_activity_b_last);
		Button bNextMonth = (Button)findViewById(R.id.gv_calendar_activity_b_next);

		mTvCalendarTitle = (TextView)findViewById(R.id.gv_calendar_activity_tv_title);
		mGvCalendar = (GridView)findViewById(R.id.gv_calendar_activity_gv_calendar);


		bLastMonth.setOnClickListener(this);
		bNextMonth.setOnClickListener(this);
		mGvCalendar.setOnItemClickListener(this);

		mDayList = new ArrayList<DayInfo>();
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onResume()
	 */
	@Override
	protected void onResume()
	{
		super.onResume();

		mThisMonthCalendar = Calendar.getInstance();
		mThisMonthCalendar.set(Calendar.DAY_OF_MONTH, 1);
		getCalendar(mThisMonthCalendar);
	}

	/**
	 * 
	 * @param calendar 
	 */
	private void getCalendar(Calendar calendar)
	{
		int lastMonthStartDay;
		int dayOfMonth;
		int thisMonthLastDay;


		mDayList.clear();

		dayOfMonth = calendar.get(Calendar.DAY_OF_WEEK);
		thisMonthLastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

		calendar.add(Calendar.MONTH, -1);
		Log.e("", calendar.get(Calendar.DAY_OF_MONTH)+"");

		lastMonthStartDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

		calendar.add(Calendar.MONTH, 1);
		Log.e("", calendar.get(Calendar.DAY_OF_MONTH)+"");

		if(dayOfMonth == SUNDAY)
		{
			dayOfMonth += 7;
		}

		lastMonthStartDay -= (dayOfMonth-1)-1;



		mTvCalendarTitle.setText(mThisMonthCalendar.get(Calendar.YEAR) + "  "
				+ (getStringMonth(mThisMonthCalendar.get(Calendar.MONTH) + 1)));

		DayInfo day;

		Log.e("DayOfMOnth", dayOfMonth+"");

		for(int i=0; i<7; i++)
		{
			day = new DayInfo();
			day.setDay(dayText[i]);
			day.setInMonth(false);
			day.setToday(false);
			day.setIsText(true);
			mDayList.add(day);


		}
		for(int i=0; i<dayOfMonth-1; i++)
		{
			int date = lastMonthStartDay+i;
			day = new DayInfo();
			day.setDay(Integer.toString(date));
			day.setInMonth(false);
			day.setToday(false);
			day.setIsText(false);
			mDayList.add(day);
		}
		for(int i=1; i <= thisMonthLastDay; i++)
		{
			day = new DayInfo();
			day.setDay(Integer.toString(i));
			day.setInMonth(true);
			if(mThisMonthCalendar.get(Calendar.YEAR)==yearOfToday
					&& mThisMonthCalendar.get(Calendar.MONTH)==monthOfToday
					&& i==dayOfToday)
			{
				day.setToday(true);
			}
			else{
				day.setToday(false);
			}
			day.setIsText(false);

			mDayList.add(day);
		}
		for(int i=1; i<42-(thisMonthLastDay+dayOfMonth-1)+1; i++)
		{
			day = new DayInfo();
			day.setDay(Integer.toString(i));
			day.setInMonth(false);
			day.setToday(false);
			day.setIsText(false);
			mDayList.add(day);
		}

		initCalendarAdapter();
	}

	/**
	 * 
	 * @param calendar
	 * @return LastMonthCalendar
	 */
	private Calendar getLastMonth(Calendar calendar)
	{
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), 1);
		calendar.add(Calendar.MONTH, -1);
		mTvCalendarTitle.setText(mThisMonthCalendar.get(Calendar.YEAR) + "  "
				+ (getStringMonth(mThisMonthCalendar.get(Calendar.MONTH) + 1)));
		return calendar;
	}

	/**
	 * �������� Calendar ������ ����������.
	 * 
	 * @param calendar
	 * @return NextMonthCalendar
	 */
	private Calendar getNextMonth(Calendar calendar)
	{
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), 1);
		calendar.add(Calendar.MONTH, +1);
		mTvCalendarTitle.setText(mThisMonthCalendar.get(Calendar.YEAR) + "  "
				+ (getStringMonth(mThisMonthCalendar.get(Calendar.MONTH) + 1)));
		return calendar;
	}

	@Override
	public void onItemClick(final AdapterView<?> parent, final View v, final int position, final long arg3)
	{
		final AlertDialog.Builder builder = new AlertDialog.Builder(this);
		final AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
		final int selectedYear = mThisMonthCalendar.get(Calendar.YEAR);
		final int selectedMonth = mThisMonthCalendar.get(Calendar.MONTH)+1;
		final String selectedDay = mDayList.get(position).getDay();
		LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
		final View layout = inflater.inflate(R.layout.addweightincalview,(ViewGroup) findViewById(R.id.addweightdialog));
		if(mDayList.get(position).isInMonth()){
			/*Toast.makeText(GVCalendarActivity.this, mThisMonthCalendar.get(Calendar.YEAR) 
				+ " " + getStringMonth(mThisMonthCalendar.get(Calendar.MONTH) + 1) + " " +
			mDayList.get(position).getDay(), Toast.LENGTH_SHORT).show();
			 */
			builder.setTitle( getStringMonth(selectedMonth) + " " +
					selectedDay + " " + selectedYear);
			builder.setMessage("Choose from the following:");
			builder.setIcon(me.croute.calendarexample.R.drawable.eft);
			
			builder.setNeutralButton("Goto Day", new DialogInterface.OnClickListener(){
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					onItemClick(parent,v,position,arg3);
				}
			});
			
			builder.setNeutralButton("Add Item", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					builder.setTitle(selectedYear 
							+ " " + getStringMonth(selectedMonth) + " " +
							selectedDay);
					builder.setMessage("Choose what you want to add");
					builder.setNeutralButton("Weight", new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							builder2.setTitle(selectedYear 
									+ " " + getStringMonth(selectedMonth) + " " +
									selectedDay);
							builder2.setMessage("Add your personal day weight");
							builder2.setIcon(me.croute.calendarexample.R.drawable.eft);
							builder2.setView(layout);
							builder2.setPositiveButton("Save", new DialogInterface.OnClickListener(){
								@Override
								public void onClick(DialogInterface dialog, int which) {
									// TODO Auto-generated method stub
									onItemClick(parent,v,position,arg3);

								}
							});
							builder2.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
								@Override
								public void onClick(DialogInterface dialog, int which) {
									// TODO Auto-generated method stub
									onItemClick(parent,v,position,arg3);
								}
							});
							builder2.show();
						}
					});
					builder.setPositiveButton("Workout", new DialogInterface.OnClickListener(){
						@Override
						public void onClick(DialogInterface dialog, int which) {
							
							Intent addWorkoutPage = new Intent(getApplicationContext(), AddWorkout.class);
							addWorkoutPage.putExtra( "eft.android.view" + ".date",  getStringMonth(selectedMonth) +
									selectedDay + selectedYear );
			                startActivity(addWorkoutPage);
							
							// TODO Auto-generated method stub
							onItemClick(parent,v,position,arg3);
						}
					});
					builder.setNegativeButton("Back", new DialogInterface.OnClickListener(){
						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							onItemClick(parent,v,position,arg3);
						}
					});
					
					builder.show();
				}
			});
			builder.setPositiveButton("History", new DialogInterface.OnClickListener(){
				
				public void onClick(DialogInterface dialog, int which) { 
			/*	public void onClick(View view) { */
					
					Intent intent = new Intent(getApplicationContext(), ExcerciseHistory.class);
	                startActivity(intent);
					
					// TODO Auto-generated method stub

				}
			});
			builder.setNegativeButton("Close", new DialogInterface.OnClickListener(){
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub

				}
			});
			builder.show();
		}
	}
	
	@Override
	public void onClick(View v)
	{
		switch(v.getId())
		{
		case R.id.gv_calendar_activity_b_last:
			mThisMonthCalendar = getLastMonth(mThisMonthCalendar);
			getCalendar(mThisMonthCalendar);
			break;
		case R.id.gv_calendar_activity_b_next:
			mThisMonthCalendar = getNextMonth(mThisMonthCalendar);
			getCalendar(mThisMonthCalendar);
			break;
		}
	}

	private void initCalendarAdapter()
	{
		mCalendarAdapter = new CalendarAdapter(this, R.layout.day, mDayList);
		mGvCalendar.setAdapter(mCalendarAdapter);
	}
	private String getStringMonth(int m){
		if(m > 0 && m < 13){
			switch(m){
			case 1:
				return "JAN";
			case 2:
				return "FEB";
			case 3: 
				return "MAR";
			case 4:
				return "APR";
			case 5: 
				return "MAY";
			case 6:
				return "JUN";
			case 7:
				return "JUL";
			case 8: 
				return "AUG";
			case 9:
				return "SEP";
			case 10:
				return "OCT";
			case 11:
				return "NOV";
			case 12:
				return "DEC";
			}

		}
		return null;


	}
}